#include "downloadcontrollerh/downloadmanagercontroller.h"
#include "downloadcontrollerh/downloaditemcontroller.h"
#include "downloadviewh/downloaditemwidget.h"
#include <QListWidgetItem>
downloadmanagercontroller::downloadmanagercontroller(QObject *parent):
    QObject(parent)
{

}



void downloadmanagercontroller::downloadRequested(QWebEngineDownloadRequest *download)
{
    Q_ASSERT(download && download->state() == QWebEngineDownloadRequest::DownloadRequested);

    QString path;
    path = setPath();

    if(downloadDir.isEmpty())
    {   //path = QFileDialog::getSaveFileName(this, tr("Save as"), QDir(download->downloadDirectory()).path());
        path = QFileDialog::getExistingDirectory(this,"请选择保存路径...","./") + '/';
        downloadDir = path;
        if (downloadDir.isEmpty())
            return;
    }
    else
        path = downloadDir;

    download->setDownloadDirectory(QFileInfo(path).path());
    download->setDownloadFileName(download->downloadFileName());

    download->accept();

    downloaditemcontroller* newcontroller = new downloaditemcontroller(download, this);
    downloaditemwidget *newwidgetitem = new downloaditemwidget(newcontroller, this);

    newcontroller->downloadFileName = download->downloadFileName();
    newcontroller->downloadFileDir = path;
    newcontroller->curTime = QDateTime::currentDateTime();

    add(newwidgetitem, newcontroller);

}
